package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactUser;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.GlideHelper;

import java.util.HashMap;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/26
 **/
public class FocusOrFansAdapter extends BaseNormalListAdapter<ContactUser, FocusOrFansAdapter.ViewHolder> {

    public FocusOrFansAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_focus_or_fans, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        ContactUser contactUser = getData(position);

        GlideHelper.setCircleImage(mContext, contactUser.getUser_img(), holder.iv, R.mipmap.icon_default_header_man);
        holder.tvName.setText(contactUser.getUsername());
        if (TextUtils.isEmpty(contactUser.getBrief())) {
            holder.tvSign.setText("暂无签名");
        } else {
            holder.tvSign.setText(contactUser.getBrief());
        }

        setOperate(holder.tvOperate, contactUser);

        holder.tvOperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HashMap<String, Object> params = new HashMap<>();
                params.put("id", contactUser.getId());
                params.put("act", contactUser.isFollowed() ? 2 : 1);

                RequestManager requestManager = RequestManager.getInstance();
                if (contactUser.isFollowed()) {
                    DialogHelper.showNormalDialog(mContext, "确定要取消关注【" + contactUser.getUsername() + "】吗？",
                            new DialogHelper.Callback() {
                                @Override
                                public void onRightClick(AlertDialog alertDialog) {
                                    super.onRightClick(alertDialog);

                                    requestManager.request(requestManager.mMessageRetrofitService.focusOrNot(RequestManager.getJsonRequestBody(params)),
                                            new BaseObserverImpl<Object>(mContext) {
                                                @Override
                                                public void onSuccess() {
                                                    super.onSuccess();

                                                    contactUser.setFollowed(!contactUser.isFollowed());

                                                    setOperate(holder.tvOperate, contactUser);
                                                }
                                            });
                                }
                            });
                } else {
                    requestManager.request(requestManager.mMessageRetrofitService.focusOrNot(RequestManager.getJsonRequestBody(params)),
                            new BaseObserverImpl<Object>(mContext) {
                                @Override
                                public void onSuccess() {
                                    super.onSuccess();

                                    contactUser.setFollowed(!contactUser.isFollowed());

                                    setOperate(holder.tvOperate, contactUser);
                                }
                            });
                }
            }
        });
    }

    private void setOperate(TextView tv, ContactUser contactUser) {
        tv.setText(contactUser.isFollowed() ? "已关注" : "关注");
        tv.setTextColor(ContextCompat.getColor(mContext, contactUser.isFollowed() ? R.color.gray_light : R.color.green_theme));
        GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadius(360);
        bg.setStroke(1, ContextCompat.getColor(mContext, contactUser.isFollowed() ? R.color.gray_light : R.color.green_theme));
        tv.setBackground(bg);
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tvName;
        TextView tvSign;
        TextView tvOperate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tvName = itemView.findViewById(R.id.tv_name);
            tvSign = itemView.findViewById(R.id.tv_sign);
            tvOperate = itemView.findViewById(R.id.tv_operate);
        }
    }
}
